﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;
using SGP;

namespace AnimationEditor
{

    public partial class Form1 : Form
    {
        //Runs the main game loop
        public bool Looping = true;

        //Current loaded Animation
        public Animation CurrentAnimation = new Animation();
        public bool HasLoaded = false;

        //Selection box data elements
        Point MouseCurrPos = new Point();
        Point MouseMoveCurrPos = new Point();
        Point MousePrevPos = new Point();
        Point DrawPanelScrollOffest = new Point();
        Point ClickedPos = new Point();
        Point MouseReleasedPoint = new Point();
        bool ColMouseMoving;
        bool SourceMouseMoving;

        bool MiddleMouseDown = false;

        //Saving 
        Image SaveImage;
        public string ImagePath;

        //DirectX Data
        public Color ClearColor = new Color();
        public Color PreviewClearColor = new Color();

        //Sprite Sheet ID
        public int SpriteSheetID = -1;

        //Preview Texture ID;
        public int PreviewImageID = -1;

        //Wrapper Storage
        CSGP_Direct3D d3d;
        CSGP_TextureManager tm;

        //List for the list box
        public List<Frame> FormFrameList = new List<Frame>();

        //Bool for whether or not we are changing colors
        public bool ColorChange = false;

        //Bool for whether or not we are changing AnchorPoints
        public bool AnchorPointChange = false;
        
        //Bool That toggles the placing of the shoulder point
        public bool ShoulderPointChange = false;

        //Bool  for whether or not we are editing event pos
        public bool EditingEvent = false;

        //Bool for playing the animation
        public bool PreviewAnimation = false;

        //Bool for paused animation
        public bool IsPreviewPaused = false;

        //Iterator for preview
        public int CurrPreviewFrame = 1;

        //Frame Timer
        public float FrameTimer = 0.0f;

        //Calculate Time
        public System.DateTime TimeThen = System.DateTime.Now;
        public System.DateTime TimeNow = System.DateTime.Now;
        public float DeltaTime = 0.0f;

        //What is currently being stored in the frame counter;
        public int FrameCountDisplayed = 0;

        //string for the saved file path
        public string SavedAnimationSetFilePath = "none";


        //Init the Form
        public Form1()
        {
            InitializeComponent();

            //Hide the advanced controls
            this.groupBox2.Visible = false;

            // Def index combo box
            this.comboBox1.SelectedIndex = 0;

            //Set the clear color value
            ClearColor = Color.Azure;
            PreviewClearColor = ClearColor;

            d3d = SGP.CSGP_Direct3D.GetInstance();
            tm = SGP.CSGP_TextureManager.GetInstance();

            //Setup Wrappers
            d3d.Initialize(this.spriteSheetWindow1, true);
            d3d.AddRenderTarget(this.previewWindow1);
            tm.Initialize(d3d.Device, d3d.Sprite);

            //Add Starting Animation to List
            AnimList.Items.Add(CurrentAnimation);
            AnimList.SelectedIndex = 0;

        }

        //Render - (Replaces draw funtion)
        public void Render()
        {

            // Clear the backbuffer
            SGP.CSGP_Direct3D.GetInstance().Clear(this.spriteSheetWindow1, ClearColor);	// Dark gray

            // Begin rendering
            SGP.CSGP_Direct3D.GetInstance().DeviceBegin();
            SGP.CSGP_Direct3D.GetInstance().SpriteBegin();
            {
                //Render the Curent frame 
                if (SpriteSheetID != -1)
                {
                    this.spriteSheetWindow1.Render(SpriteSheetID, this.DrawPanelScrollOffest);
                }

                //Draw the drawing rect for collision
                if (ColMouseMoving == true)
                {
                    //Get Mouse pos relative to the the draw window
                    // Point CurrMousePos = this.spriteSheetWindow1.GetMousePos();
                    //Calc draw rect
                    Point TempMousepos = MouseCurrPos;
                    TempMousepos.X += DrawPanelScrollOffest.X;
                    TempMousepos.Y += DrawPanelScrollOffest.Y;

                    int height = ClickedPos.Y - MouseCurrPos.Y;
                    int width = ClickedPos.X - MouseCurrPos.X;
                    Size tempSize = new Size(width * -1, height * -1);
                    Rectangle tempRect = new Rectangle(ClickedPos, tempSize);
                    // tempRect.X += DrawPanelScrollOffest.X;
                    // tempRect.Y += DrawPanelScrollOffest.Y;
                    d3d.DrawHollowRect(tempRect, Color.Red, 2);
                }
                //Draw the drawing rect for source
                if (SourceMouseMoving == true)
                {
                    //Get Mouse pos relative to the the draw window
                    // Point CurrMousePos = this.spriteSheetWindow1.GetMousePos();
                    //Calc draw rect
                    int height = ClickedPos.Y - MouseCurrPos.Y;
                    int width = ClickedPos.X - MouseCurrPos.X;
                    Size tempSize = new Size(width * -1, height * -1);
                    Rectangle tempRect = new Rectangle(ClickedPos, tempSize);
                    // tempRect.X += DrawPanelScrollOffest.X;
                    // tempRect.Y += DrawPanelScrollOffest.Y;
                    d3d.DrawHollowRect(tempRect, Color.Blue, 2);
                }

                //Draw Anchor Point Cross hair
                Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                if (TempFrame.AnchorPoint.X != 0 && TempFrame.AnchorPoint.Y != 0)
                {
                    //Draw Vert Line
                    //d3d.DrawLine(TempFrame.AnchorPoint.X + DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y - 5 + DrawPanelScrollOffest.Y, TempFrame.AnchorPoint.X + DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y + 5 + DrawPanelScrollOffest.Y, Color.Black, 2);
                    //Draw Horizontal Line
                    //d3d.DrawLine(TempFrame.AnchorPoint.X - 5 + DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y + DrawPanelScrollOffest.Y, TempFrame.AnchorPoint.X + 5 + DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y + DrawPanelScrollOffest.Y, Color.Black, 2);


                    //Draw Vert Line
                    d3d.DrawLine(TempFrame.AnchorPoint.X - DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y - 5 - DrawPanelScrollOffest.Y, TempFrame.AnchorPoint.X - DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y + 5 - DrawPanelScrollOffest.Y, Color.Black, 2);
                    //Draw Horizontal Line
                    d3d.DrawLine(TempFrame.AnchorPoint.X - 5 - DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y - DrawPanelScrollOffest.Y, TempFrame.AnchorPoint.X + 5 - DrawPanelScrollOffest.X, TempFrame.AnchorPoint.Y - DrawPanelScrollOffest.Y, Color.Black, 2);

                    //Draw the event point
                    if (TempFrame.EventPoint.X != 0 && TempFrame.EventPoint.Y != 0)
                    {
                        //Draw Vert Line For Event Point
                        d3d.DrawLine(TempFrame.EventPoint.X - DrawPanelScrollOffest.X, TempFrame.EventPoint.Y - 5 - DrawPanelScrollOffest.Y, TempFrame.EventPoint.X - DrawPanelScrollOffest.X, TempFrame.EventPoint.Y + 5 - DrawPanelScrollOffest.Y, Color.Orange, 2);
                        //Draw Horizontal Line For Event Point
                        d3d.DrawLine(TempFrame.EventPoint.X - 5 - DrawPanelScrollOffest.X, TempFrame.EventPoint.Y - DrawPanelScrollOffest.Y, TempFrame.EventPoint.X + 5 - DrawPanelScrollOffest.X, TempFrame.EventPoint.Y - DrawPanelScrollOffest.Y, Color.Orange, 2);

                    }

                    //Draw the shoulder point
                    if (TempFrame.ShoulderPoint.X != 0 && TempFrame.ShoulderPoint.Y != 0)
                    {
                        //Draw Vert Line For Event Point
                        d3d.DrawLine(TempFrame.ShoulderPoint.X - DrawPanelScrollOffest.X, TempFrame.ShoulderPoint.Y - 5 - DrawPanelScrollOffest.Y, TempFrame.ShoulderPoint.X - DrawPanelScrollOffest.X, TempFrame.ShoulderPoint.Y + 5 - DrawPanelScrollOffest.Y, Color.Green, 2);
                        //Draw Horizontal Line For Event Point
                        d3d.DrawLine(TempFrame.ShoulderPoint.X - 5 - DrawPanelScrollOffest.X, TempFrame.ShoulderPoint.Y - DrawPanelScrollOffest.Y, TempFrame.ShoulderPoint.X + 5 - DrawPanelScrollOffest.X, TempFrame.ShoulderPoint.Y - DrawPanelScrollOffest.Y, Color.Green, 2);

                    }
                }

                //Draw the selected frames col and source box
                if (FramesList.SelectedIndex != -1)
                {
                    Frame TempDrawFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                    //d3d.DrawHollowRect(TempDrawFrame.SourceRect1, Color.Blue, 2);
                    //d3d.DrawHollowRect(TempDrawFrame.CollisionRect1, Color.Red, 2);
                    this.spriteSheetWindow1.RenderBoxes(TempDrawFrame, this.DrawPanelScrollOffest);
                }

            }
            SGP.CSGP_Direct3D.GetInstance().SpriteEnd();
            SGP.CSGP_Direct3D.GetInstance().DeviceEnd();

            // Present the backbuffer to the screen
            SGP.CSGP_Direct3D.GetInstance().Present();
        }

        //Preview Window
        public void PreviwRender()
        {

            // Clear the backbuffer
            if (PreviewClearColor != ClearColor)
            {
                d3d.Clear(this.previewWindow1, PreviewClearColor);
            }
            else
            {
                d3d.Clear(this.previewWindow1, ClearColor);	// Dark gray
            }
            // Begin rendering
            d3d.DeviceBegin();
            d3d.SpriteBegin();


            //Draw the Anchor Point in the Center
            Point CenterPrevWindow = new Point();

            //Get the center point
            CenterPrevWindow.X = previewWindow1.Size.Width / 2;
            CenterPrevWindow.Y = previewWindow1.Size.Height / 2;

            //Render the Preview
            if (IsPreviewPaused == false)
            {
                //Update the frame timer
                FrameTimer += DeltaTime;

                //Get a temp of the current frame
                Frame TempFrame = (Frame)FramesList.Items[CurrPreviewFrame - 1];
                if (TempFrame.FrameDuration1 < FrameTimer)
                {
                    CurrPreviewFrame++;
                    //Check to see if we have finished animation
                    if (CurrPreviewFrame > CurrentAnimation.NumFrames1 && Looping == true)
                    {
                        CurrPreviewFrame = 1;
                    }
                    FramesList.SelectedIndex = CurrPreviewFrame - 1;
                    FrameTimer = 0.0f;
                }

            }

            //Check to see if we have finished animation
            if (CurrPreviewFrame > CurrentAnimation.NumFrames1 && Looping == true)
            {
                CurrPreviewFrame = 1;
            }
            else if (CurrPreviewFrame > CurrentAnimation.NumFrames1 && Looping == false)
            {
                this.PreviewAnimation = false;
            }

            if (PreviewAnimation == true)
            {
                //Get Temp Frame
                Frame TempFrame = (Frame)FramesList.Items[CurrPreviewFrame - 1];

                //Get offset
                Point OffSet = new Point();
                OffSet.X = TempFrame.AnchorPoint.X - TempFrame.SourceRect1.Left;
                OffSet.Y = TempFrame.AnchorPoint.Y - TempFrame.SourceRect1.Top;

                tm.Draw(SpriteSheetID, CenterPrevWindow.X - OffSet.X, CenterPrevWindow.Y - OffSet.Y, 1.0f, 1.0f, TempFrame.SourceRect1, 0.0f, 0.0f, 0.0f);
            }

            //Draw Vert Line
            d3d.DrawLine(CenterPrevWindow.X, CenterPrevWindow.Y - 5, CenterPrevWindow.X, CenterPrevWindow.Y + 5, Color.Black, 2);
            //Draw Horizontal Line
            d3d.DrawLine(CenterPrevWindow.X - 5, CenterPrevWindow.Y, CenterPrevWindow.X + 5, CenterPrevWindow.Y, Color.Black, 2);

            d3d.SpriteEnd();
            d3d.DeviceEnd();

            // Present the backbuffer to the screen
            d3d.Present();
        }



        //Update - Updates by frame
        public void Update()
        {
            if (FramesList.SelectedIndex != -1)
            {
                //Update Frames Event
                Frame TempEvFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                TempEvFrame.CurrentEvent = comboBox1.Text;
                FramesList.Items[FramesList.SelectedIndex] = TempEvFrame;
            }

            //Update Cursor 
            if (EditingEvent == true || AnchorPointChange == true || ShoulderPointChange == true)
            {
                Cursor = Cursors.Cross;
            }
            else
                Cursor = Cursors.Default;

            //Update Time
            TimeNow = System.DateTime.Now;

            //Get Subtract Prev and Cur times
            System.TimeSpan TempTime = TimeNow - TimeThen;

            //Update DeltaTime
            DeltaTime = (float)TempTime.TotalSeconds;

            //Update Prev Time
            TimeThen = TimeNow;


            //Update all of the frame index's
            for (int i = 0; i < this.FramesList.Items.Count; i++)
            {
                Frame f = (Frame)(FramesList.Items[i]);
                f.Index1 = i;
                FramesList.Items[i] = f;
            }


            //Transfer the list box to a list
            for (int i = 0; i < this.FramesList.Items.Count; i++)
            {
                this.FormFrameList.Add((Frame)FramesList.Items[i]);
            }

            //Load the current image to te texture manager
            if (HasLoaded == false && CurrentAnimation.ImagePath1 != "none")
            {
                SpriteSheetID = SGP.CSGP_TextureManager.GetInstance().LoadTexture(CurrentAnimation.ImagePath1);
                HasLoaded = true;
            }

            //Update the Animations number of frames
            CurrentAnimation.NumFrames1 = FramesList.Items.Count;

            //Auto selection of 0 index of the frames list.
            if (FramesList.SelectedIndex == -1 && FramesList.Items.Count > 0)
                FramesList.SelectedIndex = 0;

            //Add one frame is list is currently empty.
            if (FramesList.Items.Count == 0)
                this.button2_Click(this, null);

            //Update Frame counter
            if (this.FrameCountDisplayed != this.CurrPreviewFrame)
                this.toolStripTextBox1.Text = "Current Frame = " + CurrPreviewFrame;

        }

        //Exit the program
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Clean Up
            if (SpriteSheetID != -1)
                tm.UnloadTexture(SpriteSheetID);

            tm.Terminate();
            d3d.Terminate();

            this.Close();
        }


        private void loadSpriteSheetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog odlg = new OpenFileDialog();
            if (odlg.ShowDialog() == DialogResult.OK)
            {
                if (SpriteSheetID != -1)
                {
                    tm.UnloadTexture(SpriteSheetID);
                    SpriteSheetID = -1;
                    HasLoaded = false;
                }
                //Create image
                SaveImage = Image.FromFile(odlg.FileName);

                //File Path Name
                ImagePath = Path.GetFileName(odlg.FileName);

                CurrentAnimation.ImagePath1 = odlg.FileName;
            }
        }

        //Clean up
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Looping = false;
        }

        //Drag selection
        private void spriteSheetWindow1_MouseDown(object sender, MouseEventArgs e)
        {
            if (ColorChange == false && AnchorPointChange == false && EditingEvent == false && ShoulderPointChange == false)
            {
                ClickedPos = e.Location;
                //ClickedPos.X += DrawPanelScrollOffest.X;
                //ClickedPos.Y += DrawPanelScrollOffest.Y;
                if (e.Button == MouseButtons.Left)
                    SourceMouseMoving = true;
                else if (e.Button == MouseButtons.Right)
                    ColMouseMoving = true;
            }
            if (e.Button == MouseButtons.Middle)
            {
                if (MiddleMouseDown == false)
                {
                    this.MiddleMouseDown = true;
                    MouseMoveCurrPos = e.Location;
                    //MouseMoveCurrPos.X += DrawPanelScrollOffest.X;
                    //MouseMoveCurrPos.Y += DrawPanelScrollOffest.Y;

                    MousePrevPos = e.Location;
                    //MousePrevPos.X += DrawPanelScrollOffest.X;
                    //MousePrevPos.Y += DrawPanelScrollOffest.Y;
                }

            }



        }

        private void spriteSheetWindow1_MouseUp(object sender, MouseEventArgs e)
        {
            if (ColorChange == false && AnchorPointChange == false && EditingEvent == false && ShoulderPointChange == false)
            {
                MouseReleasedPoint = e.Location;
                //MouseReleasedPoint.X += DrawPanelScrollOffest.X;
                //MouseReleasedPoint.Y += DrawPanelScrollOffest.Y;

                if (e.Button == MouseButtons.Left)
                    CalculateSourceRect();
                else if (e.Button == MouseButtons.Right)
                    CalculateColRect();

                //Release draw rect
                ColMouseMoving = false;
                SourceMouseMoving = false;
            }
            else
            {
                ColorChange = false;
                AnchorPointChange = false;
                EditingEvent = false;
                ShoulderPointChange = false;
            }
            MiddleMouseDown = false;
        }

        public void CalculateColRect()
        {

            int height = ClickedPos.Y - MouseReleasedPoint.Y;
            int width = ClickedPos.X - MouseReleasedPoint.X;
            Size tempSize = new Size(-width, -height);
            Rectangle temp = new Rectangle(ClickedPos, tempSize);
            temp.X += DrawPanelScrollOffest.X;
            temp.Y += DrawPanelScrollOffest.Y;

            //Set Selected Col Rect
            if (FramesList.SelectedIndex != -1)
            {
                Frame f = (Frame)(this.FramesList.Items[FramesList.SelectedIndex]);
                f.CollisionRect1 = temp;
                this.FramesList.Items[FramesList.SelectedIndex] = f;
            }

            //Clear the points 
            MouseReleasedPoint = Point.Empty;
            ClickedPos = Point.Empty;

        }

        public void CalculateSourceRect()
        {
            int height = ClickedPos.Y - MouseReleasedPoint.Y;
            int width = ClickedPos.X - MouseReleasedPoint.X;
            Size tempSize = new Size(-width, -height);
            Rectangle temp = new Rectangle(ClickedPos, tempSize);
            temp.X += DrawPanelScrollOffest.X;
            temp.Y += DrawPanelScrollOffest.Y;

            //Set Selected index Source Rect
            if (FramesList.SelectedIndex != -1)
            {
                Frame f = (Frame)(this.FramesList.Items[FramesList.SelectedIndex]);
                f.SourceRect1 = temp;

                //Calculate the mid points
                f.AnchorPoint.Y = (f.SourceRect1.Bottom - f.SourceRect1.Top) / 2;
                f.AnchorPoint.X = (f.SourceRect1.Right - f.SourceRect1.Left) / 2;

                //Add the midpoints to center
                f.AnchorPoint.X += f.SourceRect1.Left;
                f.AnchorPoint.Y += f.SourceRect1.Top;


                this.FramesList.Items[FramesList.SelectedIndex] = f;
            }



            //Clear the points 
            MouseReleasedPoint = Point.Empty;
            ClickedPos = Point.Empty;

        }

        //Store the current mouse pos
        private void spriteSheetWindow1_MouseMove(object sender, MouseEventArgs e)
        {

            MouseMoveCurrPos = e.Location;

            if (MiddleMouseDown == true)
            {
                DrawPanelScrollOffest.X += MouseMoveCurrPos.X - MousePrevPos.X;
                DrawPanelScrollOffest.Y += MouseMoveCurrPos.Y - MousePrevPos.Y;
            }

            MousePrevPos = e.Location;

            MouseCurrPos = e.Location;

        }

        //Add a frame button
        private void button2_Click(object sender, EventArgs e)
        {
            Frame tempFrame = new Frame();
            this.FramesList.Items.Add(tempFrame);
            this.FramesList.SelectedIndex = FramesList.Items.Count - 1;
        }

        //Add Animation Button
        private void button3_Click(object sender, EventArgs e)
        {
            Animation TempAnim = new Animation();
            TempAnim.Index = AnimList.Items.Count;
            this.AnimList.Items.Add(TempAnim);
            this.AnimList.SelectedIndex = AnimList.Items.Count - 1;
        }

        //When ever a new anim is selected
        private void AnimList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.AnimList.SelectedIndex != -1)
            {
                CurrentAnimation.FrameList.Clear();
                //Store Old Frames List of prev selected anim before clearing.
                for (int i = 0; i < FramesList.Items.Count; i++)
                {
                    CurrentAnimation.FrameList.Add((Frame)FramesList.Items[i]);
                }

                //Unload old animation
                if (SpriteSheetID != -1)
                {
                    tm.UnloadTexture(SpriteSheetID);
                    SpriteSheetID = -1;
                }

                //Switch Current Animation
                CurrentAnimation = (Animation)AnimList.Items[AnimList.SelectedIndex];

                //Load new SpriteSheet
                if (CurrentAnimation.ImagePath1 != "none")
                    SpriteSheetID = tm.LoadTexture(CurrentAnimation.ImagePath1);

                //Clear the Frames list
                FramesList.Items.Clear();

                if (CurrentAnimation.NumFrames1 != 0)
                {
                    for (int i = 0; i < CurrentAnimation.FrameList.Count; i++)
                    {
                        FramesList.Items.Add((Frame)CurrentAnimation.FrameList[i]);
                    }
                }

                //Unload last anim ImageID
                if (this.PreviewImageID != -1)
                    tm.UnloadTexture(PreviewImageID);

                //Load new Anim ID
                if (CurrentAnimation.ImagePath1 != "none")
                    PreviewImageID = tm.LoadTexture(CurrentAnimation.ImagePath1);

                //Reset Current View
                CurrPreviewFrame = 1;
                PreviewAnimation = false;
                IsPreviewPaused = true;

                //Update the ignored color 
                if (CurrentAnimation.IgnoredRed != 0 || CurrentAnimation.IgnoredGreen != 0 || CurrentAnimation.IgnoredBlue != 0)
                {
                    this.button4.BackColor = Color.FromArgb(CurrentAnimation.IgnoredRed, CurrentAnimation.IgnoredGreen, CurrentAnimation.IgnoredBlue);
                    this.PreviewClearColor = button4.BackColor;
                }

            }
        }

        //For the click event on the drawing window
        private void spriteSheetWindow1_Click(object sender, EventArgs e)
        {
            if (ColorChange == true && this.CurrentAnimation.ImagePath1 != "none")
            {
                //Create a Bitmap
                Bitmap ColorSelectionBitmap = new Bitmap(this.CurrentAnimation.ImagePath1);

                //Access the color at the point
                Color pixelColor = ColorSelectionBitmap.GetPixel(MouseCurrPos.X, MouseCurrPos.Y);

                this.button4.BackColor = pixelColor;
                PreviewClearColor = pixelColor;

                CurrentAnimation.IgnoredBlue = pixelColor.B;
                CurrentAnimation.IgnoredRed = pixelColor.R;
                CurrentAnimation.IgnoredGreen = pixelColor.G;
            }

            if (AnchorPointChange == true)
            {
                Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                TempFrame.AnchorPoint = this.MouseCurrPos;
                TempFrame.AnchorPoint.X += this.DrawPanelScrollOffest.X;
                TempFrame.AnchorPoint.Y += this.DrawPanelScrollOffest.Y;
                FramesList.Items[FramesList.SelectedIndex] = TempFrame;
            }

            if (EditingEvent == true)
            {
                Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                TempFrame.EventPoint = MouseCurrPos;
                TempFrame.EventPoint.X += this.DrawPanelScrollOffest.X;
                TempFrame.EventPoint.Y += this.DrawPanelScrollOffest.Y;
                FramesList.Items[FramesList.SelectedIndex] = TempFrame;
            }

            if (ShoulderPointChange == true)
            {
                Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                TempFrame.ShoulderPoint = this.MouseCurrPos;
                TempFrame.ShoulderPoint.X += this.DrawPanelScrollOffest.X;
                TempFrame.ShoulderPoint.Y += this.DrawPanelScrollOffest.Y;
                FramesList.Items[FramesList.SelectedIndex] = TempFrame;
            }

            // ColorChange = false;
        }

        //Change color button
        private void button4_Click(object sender, EventArgs e)
        {
            ColorChange = true;
        }

        //Edit Anchor point button
        private void button1_Click(object sender, EventArgs e)
        {
            //Add an anchor point
            AnchorPointChange = true;
        }

        private void FramesList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FramesList.SelectedIndex != -1)
            {
                //Update the anchorpoint numeric updowns
                Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                this.numericUpDown1.Value = TempFrame.AnchorPoint.X;
                this.numericUpDown2.Value = TempFrame.AnchorPoint.Y;

                //Update the Adjustment Numerics
                this.numericUpDown4.Value = TempFrame.CollisionRect1.Left;
                this.numericUpDown5.Value = TempFrame.CollisionRect1.Top;
                this.numericUpDown6.Value = TempFrame.SourceRect1.Left;
                this.numericUpDown7.Value = TempFrame.SourceRect1.Top;

                //Update the duration numeric updowns
                this.numericUpDown3.Value = (decimal)TempFrame.FrameDuration1;
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            TempFrame.AnchorPoint.X = (int)this.numericUpDown1.Value;
            FramesList.Items[FramesList.SelectedIndex] = TempFrame;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            TempFrame.AnchorPoint.Y = (int)this.numericUpDown2.Value;
            FramesList.Items[FramesList.SelectedIndex] = TempFrame;
        }

        //Play Preview button
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (this.CurrentAnimation.ImagePath1 != "none")
            {
                this.PreviewAnimation = true;
                this.IsPreviewPaused = false;
                FrameTimer = 0.0f;
                if (PreviewImageID == -1)
                    PreviewImageID = tm.LoadTexture(CurrentAnimation.ImagePath1);
                FramesList.SelectedIndex = CurrPreviewFrame - 1;
            }
        }

        //Duration Numeric Up Down
        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            //Update the current frames duration
            Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            TempFrame.FrameDuration1 = (float)numericUpDown3.Value;
            FramesList.Items[FramesList.SelectedIndex] = TempFrame;
        }

        //Stop button
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.IsPreviewPaused = true;
        }

        //Skip Back Button
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CurrPreviewFrame--;
            if (CurrPreviewFrame < 1)
                CurrPreviewFrame = FramesList.Items.Count;
            this.FramesList.SelectedIndex = CurrPreviewFrame - 1;
        }

        //Skip Forward Button
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            CurrPreviewFrame++;
            if (CurrPreviewFrame > FramesList.Items.Count)
                CurrPreviewFrame = 1;

            this.FramesList.SelectedIndex = CurrPreviewFrame - 1;
        }


        //Save Animation
        void SaveAnimationSet(bool SaveAs)
        {

            //Are we set up to write?
            bool WritingOK = false;

            //Are they saving as?
            if (SaveAs == true || SavedAnimationSetFilePath == "none")
            {
                SaveFileDialog sdlg = new SaveFileDialog();
                sdlg.Filter = "All Files|*.*|Xml Files|*.xml";
                sdlg.DefaultExt = ".xml";

                if (sdlg.ShowDialog() == DialogResult.OK)
                {
                    SavedAnimationSetFilePath = sdlg.FileName;
                    WritingOK = true;
                }

            }
            else if (SaveAs == false && SavedAnimationSetFilePath != "none")
            {
                WritingOK = true;
            }



            //Is it okay to write?
            if (WritingOK == true)
            {
                //Create the root
                XElement xRoot = new XElement("AnimationSet");

                foreach (Animation a in AnimList.Items)
                {
                    //Create Each animation
                    XElement xAnimation = new XElement("Animation");
                    xRoot.Add(xAnimation);

                    //Frames count element
                    XElement xNumFrames = new XElement("numFrames");
                    xAnimation.Add(xNumFrames);

                    //Frames count attribute
                    XAttribute xNumFramesAtt = new XAttribute("num", FramesList.Items.Count);
                    xNumFrames.Add(xNumFramesAtt);

                    //Ignored Color element
                    XElement xIgnoredColor = new XElement("IgnoredColor");
                    xAnimation.Add(xIgnoredColor);

                    XAttribute xRed = new XAttribute("r", a.IgnoredRed);
                    xIgnoredColor.Add(xRed);

                    XAttribute xGreen = new XAttribute("g", a.IgnoredGreen);
                    xIgnoredColor.Add(xGreen);

                    XAttribute xBlue = new XAttribute("b", a.IgnoredBlue);
                    xIgnoredColor.Add(xBlue);

                    //Image Path
                    XElement xPathString = new XElement("ImagePath", ImagePath);
                    xAnimation.Add(xPathString);

                    //Add a frames list
                    XElement xFrames = new XElement("FramesList");
                    xAnimation.Add(xFrames);


                    //Store all of the current animations frames
                    foreach (Frame f in FramesList.Items)
                    {
                        //Add frame to the list
                        XElement xFrame = new XElement("Frame");
                        xFrames.Add(xFrame);

                        //Add Anchor Point
                        XElement xFrameAnchor = new XElement("AnchorPoint");
                        xFrame.Add(xFrameAnchor);

                        //Add Anchor Atts
                        XAttribute xAnchorXAtt = new XAttribute("x", f.AnchorPoint.X);
                        xFrameAnchor.Add(xAnchorXAtt);

                        XAttribute xAnchorYAtt = new XAttribute("y", f.AnchorPoint.Y);
                        xFrameAnchor.Add(xAnchorYAtt);

                        //Hit Box
                        XElement xHitBox = new XElement("HitBox");
                        xFrame.Add(xHitBox);

                        XAttribute xHitLeft = new XAttribute("left", f.CollisionRect1.Left);
                        xHitBox.Add(xHitLeft);

                        XAttribute xHitTop = new XAttribute("top", f.CollisionRect1.Top);
                        xHitBox.Add(xHitTop);

                        XAttribute xHitRight = new XAttribute("right", f.CollisionRect1.Right);
                        xHitBox.Add(xHitRight);

                        XAttribute xHitBottom = new XAttribute("bottom", f.CollisionRect1.Bottom);
                        xHitBox.Add(xHitBottom);

                        //Source Rect
                        XElement xSourceBox = new XElement("SourceBox");
                        xFrame.Add(xSourceBox);

                        XAttribute xSourceLeft = new XAttribute("left", f.SourceRect1.Left);
                        xSourceBox.Add(xSourceLeft);

                        XAttribute xSourceTop = new XAttribute("top", f.SourceRect1.Top);
                        xSourceBox.Add(xSourceTop);

                        XAttribute xSourceRight = new XAttribute("right", f.SourceRect1.Right);
                        xSourceBox.Add(xSourceRight);

                        XAttribute xSourceBottom = new XAttribute("bottom", f.SourceRect1.Bottom);
                        xSourceBox.Add(xSourceBottom);

                        //Add frame duration
                        XElement xFrameDuration = new XElement("duration");
                        xFrame.Add(xFrameDuration);

                        XAttribute xFrameDurAtt = new XAttribute("dur", f.FrameDuration1);
                        xFrameDuration.Add(xFrameDurAtt);

                        //Add the Event
                        XElement xEvent = new XElement("Event", f.CurrentEvent);
                        xFrame.Add(xEvent);

                        //Add Event Point
                        XElement xEventPoint = new XElement("EventPoint");
                        xFrame.Add(xEventPoint);

                        XAttribute xEventXPoint = new XAttribute("EventX", f.EventPoint.X);
                        xEventPoint.Add(xEventXPoint);

                        XAttribute xEventYPoint = new XAttribute("EventY", f.EventPoint.Y);
                        xEventPoint.Add(xEventYPoint);

                    }

                }
                //Write out to file. 
                xRoot.Save(this.SavedAnimationSetFilePath);
                AnimList.SelectedIndex = 0;
            }
        }

        //Load Animation
        void LoadAnimationSet()
        {
            //Clear the current lists
            this.FramesList.Items.Clear();
            this.AnimList.Items.Clear();

            //Reset Stuff

            //Load in new stuff
            OpenFileDialog odlg = new OpenFileDialog();
            odlg.Filter = "All Files|*.*|Xml Files|*.xml";
            odlg.FilterIndex = 2;

            if (DialogResult.OK == odlg.ShowDialog())
            {
                XElement xRoot = XElement.Load(odlg.FileName);

                string TempPath = Path.GetDirectoryName(odlg.FileName);

                IEnumerable<XElement> xAnimations = xRoot.Elements();


                foreach (XElement xAnimation in xAnimations)
                {
                    Animation LoadedTempAnim = new Animation();

                    //Get Frames Count
                    XElement xNumFrames = xAnimation.Element("numFrames");
                    XAttribute xAttNumFrames = xNumFrames.Attribute("num");
                    int LoadedNumFrames = Convert.ToInt32(xAttNumFrames.Value);

                    //Get Ignored Color
                    XElement xIgnoredColor = xAnimation.Element("IgnoredColor");
                    XAttribute xAttIgnoredColorR = xIgnoredColor.Attribute("r");
                    XAttribute xAttIgnoredColorG = xIgnoredColor.Attribute("g");
                    XAttribute xAttIgnoredColorB = xIgnoredColor.Attribute("b");

                    int LoadedIgnoredRed = Convert.ToInt32(xAttIgnoredColorR.Value);
                    int LoadedIgnoredGreen = Convert.ToInt32(xAttIgnoredColorG.Value);
                    int LoadedIgnoredBlue = Convert.ToInt32(xAttIgnoredColorB.Value);

                    //Get Image path
                    XElement xImagePath = xAnimation.Element("ImagePath");
                    string LoadedImagePath = xImagePath.Value;

                    //Frames List
                    XElement xFramesList = xAnimation.Element("FramesList");

                    //To iterate
                    IEnumerable<XElement> xFrames = xFramesList.Elements();

                    //Loop through the frames
                    foreach (XElement xFrame in xFrames)
                    {
                        Frame tempFrame = new Frame();

                        //Get Anchor Point
                        XElement xAnchorPoint = xFrame.Element("AnchorPoint");
                        XAttribute xAnchorPointAttx = xAnchorPoint.Attribute("x");
                        XAttribute xAnchorPointAtty = xAnchorPoint.Attribute("y");

                        int LoadedAnchorX = Convert.ToInt32(xAnchorPointAttx.Value);
                        int LoadedAnchorY = Convert.ToInt32(xAnchorPointAtty.Value);

                        //Hit box
                        XElement xHitBox = xFrame.Element("HitBox");
                        XAttribute xHitBoxAttLeft = xHitBox.Attribute("left");
                        XAttribute xHitBoxAttTop = xHitBox.Attribute("top");
                        XAttribute xHitBoxAttRight = xHitBox.Attribute("right");
                        XAttribute xHitBoxAttBottom = xHitBox.Attribute("bottom");

                        int LoadedHitLeft = Convert.ToInt32(xHitBoxAttLeft.Value);
                        int LoadedHitTop = Convert.ToInt32(xHitBoxAttTop.Value);
                        int LoadedHitRight = Convert.ToInt32(xHitBoxAttRight.Value);
                        int LoadedHitBottom = Convert.ToInt32(xHitBoxAttBottom.Value);

                        //Source
                        XElement xSource = xFrame.Element("SourceBox");
                        XAttribute xSourceAttLeft = xSource.Attribute("left");
                        XAttribute xSourceAttTop = xSource.Attribute("top");
                        XAttribute xSourceAttRight = xSource.Attribute("right");
                        XAttribute xSourceAttBottom = xSource.Attribute("bottom");

                        int LoadedSourceLeft = Convert.ToInt32(xSourceAttLeft.Value);
                        int LoadedSourceTop = Convert.ToInt32(xSourceAttTop.Value);
                        int LoadedSourceRight = Convert.ToInt32(xSourceAttRight.Value);
                        int LoadedSourceBottom = Convert.ToInt32(xSourceAttBottom.Value);

                        //Duration 
                        XElement xDuration = xFrame.Element("duration");
                        XAttribute xDurrAtt = xDuration.Attribute("dur");
                        float LoadedDuration = (float)Convert.ToDouble(xDurrAtt.Value);

                        //Get the Shoulder point
                        XElement xShoulder = xFrame.Element("ShoulderPoint");
                        XAttribute xShoulderX = xShoulder.Attribute("ShoulderX");
                        XAttribute xShoulderY = xShoulder.Attribute("ShoulderY");

                        int LoadedShoulderX = Convert.ToInt32(xShoulderX.Value);
                        int LoadedShoulderY = Convert.ToInt32(xShoulderY.Value);

                        tempFrame.ShoulderPoint.X = LoadedShoulderX;
                        tempFrame.ShoulderPoint.Y = LoadedShoulderY;

                        //Add the Event
                        //XElement xEvent = new XElement("Event", f.CurrentEvent);
                        //xFrame.Add(xEvent);
                       // XElement xEvent = xFrame.Element("Event");
                        //string LoadedEvent = xEvent.Value;

                        //Assign Anchor
                        tempFrame.AnchorPoint.X = LoadedAnchorX;
                        tempFrame.AnchorPoint.Y = LoadedAnchorY;

                        //Assign Event 
                       // tempFrame.CurrentEvent = LoadedEvent;

                        //Calculate and Assign coll rect
                        int ColRectWidth = LoadedHitRight - LoadedHitLeft;
                        int ColRectHeight = LoadedHitBottom - LoadedHitTop;

                        tempFrame.CollisionRect1 = new Rectangle(LoadedHitLeft, LoadedHitTop, ColRectWidth, ColRectHeight);

                        //Calculate and Assign source rect
                        int SourceRectWidth = LoadedSourceRight - LoadedSourceLeft;
                        int SourceRectHeight = LoadedSourceBottom - LoadedSourceTop;

                        tempFrame.SourceRect1 = new Rectangle(LoadedSourceLeft, LoadedSourceTop, SourceRectWidth, SourceRectHeight);

                        //Assign Duration
                        tempFrame.FrameDuration1 = LoadedDuration;

                        //TODO:: Load in event data

                        LoadedTempAnim.FrameList.Add(tempFrame);

                    }
                    LoadedTempAnim.IgnoredBlue = LoadedIgnoredBlue;
                    LoadedTempAnim.IgnoredGreen = LoadedIgnoredGreen;
                    LoadedTempAnim.IgnoredRed = LoadedIgnoredRed;

                    ImagePath = LoadedImagePath;
                    //string ImageSavePath = "../../../../SMART/" + ImagePath;
                    string ImageSavePath = TempPath + "\\" + LoadedImagePath;

                    LoadedTempAnim.ImagePath1 = ImageSavePath;

                    LoadedTempAnim.NumFrames1 = LoadedNumFrames;
                    LoadedTempAnim.Index = AnimList.Items.Count;
                    this.AnimList.Items.Add(LoadedTempAnim);
                }

                //update the frame list
                AnimList.SelectedIndex = 0;
                //Animation TempAnimation = (Animation)AnimList.Items[AnimList.SelectedIndex];
                //for(int i = 0; i < TempAnimation.NumFrames1; i++)
                //{
                //    this.FramesList.Items.Add(TempAnimation.FrameList[i]);
                //}


            }
        }

        //New Animation Set
        void NewAnimationSet()
        {
            //Ask if they would like to save

            //Clear both Lists and reset need varriables

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAnimationSet(true);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.LoadAnimationSet();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveAnimationSet(false);
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.CurrPreviewFrame = 1;
            this.FramesList.SelectedIndex = 0;
        }

        private void exportAnimationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Are they saving as?
            SaveFileDialog sdlg = new SaveFileDialog();
            sdlg.Filter = "All Files|*.*|Xml Files|*.xml";
            sdlg.DefaultExt = ".xml";

            if (sdlg.ShowDialog() == DialogResult.OK)
            {
                SavedAnimationSetFilePath = sdlg.FileName;

                string TempPath = Path.GetDirectoryName(SavedAnimationSetFilePath);


                //Create the root
                XElement xRoot = new XElement("AnimationSet");

                Animation a = (Animation)AnimList.Items[AnimList.SelectedIndex];

                //Create Each animation
                XElement xAnimation = new XElement("Animation");
                xRoot.Add(xAnimation);

                //Frames count element
                XElement xNumFrames = new XElement("numFrames");
                xAnimation.Add(xNumFrames);

                //Frames count attribute
                XAttribute xNumFramesAtt = new XAttribute("num", FramesList.Items.Count);
                xNumFrames.Add(xNumFramesAtt);

                //Ignored Color element
                XElement xIgnoredColor = new XElement("IgnoredColor");
                xAnimation.Add(xIgnoredColor);

                XAttribute xRed = new XAttribute("r", a.IgnoredRed);
                xIgnoredColor.Add(xRed);

                XAttribute xGreen = new XAttribute("g", a.IgnoredGreen);
                xIgnoredColor.Add(xGreen);

                XAttribute xBlue = new XAttribute("b", a.IgnoredBlue);
                xIgnoredColor.Add(xBlue);

                //Image Path
                XElement xPathString = new XElement("ImagePath", ImagePath);
                xAnimation.Add(xPathString);

                //Add a frames list
                XElement xFrames = new XElement("FramesList");
                xAnimation.Add(xFrames);


                //Store all of the current animations frames
                foreach (Frame f in FramesList.Items)
                {
                    //Add frame to the list
                    XElement xFrame = new XElement("Frame");
                    xFrames.Add(xFrame);

                    //Add Anchor Point
                    XElement xFrameAnchor = new XElement("AnchorPoint");
                    xFrame.Add(xFrameAnchor);

                    //Add Anchor Atts
                    XAttribute xAnchorXAtt = new XAttribute("x", f.AnchorPoint.X);
                    xFrameAnchor.Add(xAnchorXAtt);

                    XAttribute xAnchorYAtt = new XAttribute("y", f.AnchorPoint.Y);
                    xFrameAnchor.Add(xAnchorYAtt);

                    //Hit Box
                    XElement xHitBox = new XElement("HitBox");
                    xFrame.Add(xHitBox);

                    XAttribute xHitLeft = new XAttribute("left", f.CollisionRect1.Left);
                    xHitBox.Add(xHitLeft);

                    XAttribute xHitTop = new XAttribute("top", f.CollisionRect1.Top);
                    xHitBox.Add(xHitTop);

                    XAttribute xHitRight = new XAttribute("right", f.CollisionRect1.Right);
                    xHitBox.Add(xHitRight);

                    XAttribute xHitBottom = new XAttribute("bottom", f.CollisionRect1.Bottom);
                    xHitBox.Add(xHitBottom);

                    //Source Rect
                    XElement xSourceBox = new XElement("SourceBox");
                    xFrame.Add(xSourceBox);

                    //Temp source for offset
                    Rectangle tempRectOff = f.SourceRect1;
                    //tempRectOff.X += DrawPanelScrollOffest.X;
                    //tempRectOff.Y += DrawPanelScrollOffest.Y;

                    XAttribute xSourceLeft = new XAttribute("left", tempRectOff.Left);
                    xSourceBox.Add(xSourceLeft);

                    XAttribute xSourceTop = new XAttribute("top", tempRectOff.Top);
                    xSourceBox.Add(xSourceTop);

                    XAttribute xSourceRight = new XAttribute("right", tempRectOff.Right);
                    xSourceBox.Add(xSourceRight);

                    XAttribute xSourceBottom = new XAttribute("bottom", tempRectOff.Bottom);
                    xSourceBox.Add(xSourceBottom);

                    //Add frame duration
                    XElement xFrameDuration = new XElement("duration");
                    xFrame.Add(xFrameDuration);

                    XAttribute xFrameDurAtt = new XAttribute("dur", f.FrameDuration1);
                    xFrameDuration.Add(xFrameDurAtt);

                    //Add the Event
                    XElement xEvent = new XElement("Event", f.CurrentEvent);
                    xFrame.Add(xEvent);

                    //Add Event Point
                    XElement xEventPoint = new XElement("EventPoint");
                    xFrame.Add(xEventPoint);

                    XAttribute xEventXPoint = new XAttribute("EventX", f.EventPoint.X);
                    xEventPoint.Add(xEventXPoint);

                    XAttribute xEventYPoint = new XAttribute("EventY", f.EventPoint.Y);
                    xEventPoint.Add(xEventYPoint);

                    //Add Shoulder Point
                    XElement xShoulderPoint = new XElement("ShoulderPoint");
                    xFrame.Add(xShoulderPoint);

                    XAttribute xShoulderXPoint = new XAttribute("ShoulderX", f.ShoulderPoint.X);
                    xShoulderPoint.Add(xShoulderXPoint);

                    XAttribute xShoulderYPoint = new XAttribute("ShoulderY", f.ShoulderPoint.Y);
                    xShoulderPoint.Add(xShoulderYPoint);

                }

                //TODO:: On Launch get the resource folder. 
                //Image Save Path 
                //string ImageSavePath = "../../../../SMART/Textures/" + ImagePath;
                TempPath += "\\" + ImagePath;
                try
                {
                    SaveImage.Save(TempPath);
                }
                catch (Exception) { }

                //Write out to file. 
                xRoot.Save(this.SavedAnimationSetFilePath);
                AnimList.SelectedIndex = 0;
            }
        }

        private void numericUpDown2_ValueChanged_1(object sender, EventArgs e)
        {
            Frame TempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            TempFrame.AnchorPoint.Y = (int)this.numericUpDown2.Value;
            FramesList.Items[FramesList.SelectedIndex] = TempFrame;
        }

        //Display Advanced Editing Option
        private void button5_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = !groupBox2.Visible;
        }

        //Add and event.
        private void button6_Click(object sender, EventArgs e)
        {
            EditingEvent = true;
        }

        //Remove Frame
        private void button7_Click(object sender, EventArgs e)
        {
            int SelectedIndexStart = FramesList.SelectedIndex;
            FramesList.Items.Remove(FramesList.Items[FramesList.SelectedIndex]);
            if (FramesList.SelectedIndex - 1 > 0)
                FramesList.SelectedIndex = SelectedIndexStart - 1;
        }

        //Remove Animation
        private void button8_Click(object sender, EventArgs e)
        {
            int SelectedIndexStart = AnimList.SelectedIndex;
            AnimList.Items.Remove(AnimList.Items[AnimList.SelectedIndex]);
            if (AnimList.SelectedIndex - 1 > 0)
                AnimList.SelectedIndex = SelectedIndexStart - 1;
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            Frame tempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            int ColRectWidth = tempFrame.CollisionRect1.Right - tempFrame.CollisionRect1.Left;
            int ColRectHeight = tempFrame.CollisionRect1.Bottom - tempFrame.CollisionRect1.Top;

            tempFrame.CollisionRect1 = new Rectangle((int)numericUpDown4.Value, tempFrame.CollisionRect1.Top, ColRectWidth, ColRectHeight);

            FramesList.Items[FramesList.SelectedIndex] = tempFrame;
        }

        private void numericUpDown5_ValueChanged(object sender, EventArgs e)
        {
            Frame tempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            int ColRectWidth = tempFrame.CollisionRect1.Right - tempFrame.CollisionRect1.Left;
            int ColRectHeight = tempFrame.CollisionRect1.Bottom - tempFrame.CollisionRect1.Top;

            tempFrame.CollisionRect1 = new Rectangle(tempFrame.CollisionRect1.Left, (int)numericUpDown5.Value, ColRectWidth, ColRectHeight);

            FramesList.Items[FramesList.SelectedIndex] = tempFrame;
        }

        private void numericUpDown6_ValueChanged(object sender, EventArgs e)
        {
            Frame tempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            int SourceRectWidth = tempFrame.SourceRect1.Right - tempFrame.SourceRect1.Left;
            int SourceRectHeight = tempFrame.SourceRect1.Bottom - tempFrame.SourceRect1.Top;

            tempFrame.SourceRect1 = new Rectangle((int)numericUpDown6.Value, tempFrame.SourceRect1.Top, SourceRectWidth, SourceRectHeight);

            FramesList.Items[FramesList.SelectedIndex] = tempFrame;
        }

        private void numericUpDown7_ValueChanged(object sender, EventArgs e)
        {
            Frame tempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            int SourceRectWidth = tempFrame.SourceRect1.Right - tempFrame.SourceRect1.Left;
            int SourceRectHeight = tempFrame.SourceRect1.Bottom - tempFrame.SourceRect1.Top;

            tempFrame.SourceRect1 = new Rectangle(tempFrame.SourceRect1.Left, (int)numericUpDown7.Value, SourceRectWidth, SourceRectHeight);

            FramesList.Items[FramesList.SelectedIndex] = tempFrame;
        }

        private void button9_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < FramesList.Items.Count; i++)
            {
                Frame tempFrame = (Frame)FramesList.Items[i];
                tempFrame.FrameDuration1 = (float)numericUpDown8.Value;
                FramesList.Items[i] = tempFrame;
            }
            Frame UpdateFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
            numericUpDown3.Value = (decimal)UpdateFrame.FrameDuration1;
        }

        //Clear All
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {

            //Unload Texture
            if (SpriteSheetID != -1)
                tm.UnloadTexture(this.SpriteSheetID);
            SpriteSheetID = -1;

            AnimList.Items.Clear();
            FramesList.Items.Clear();

            //Add new anim
            Animation temp = new Animation();
            AnimList.Items.Add(temp);
            AnimList.SelectedIndex = 0;


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FramesList.SelectedIndex != -1)
            {
                Frame tempFrame = (Frame)FramesList.Items[FramesList.SelectedIndex];
                tempFrame.CurrentEvent = comboBox1.Text;
                FramesList.Items[FramesList.SelectedIndex] = tempFrame;
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            ShoulderPointChange = true;
        }
    }
}
